package com.future.mvc.web.exception;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;

/**
 * <P>
 * 全局异常处理器
 * 处理局部异常不能处理的异常
 *
 * </p>
 *
 * @author yinhao
 * @version 1.0
 * @since 2024/9/8 20:19
 */
@ControllerAdvice
public class FutureGlobalException {

    @Autowired
    private HttpServletRequest request;

    // @ExceptionHandler({ArrayIndexOutOfBoundsException.class, NumberFormatException.class, ClassCastException.class, AgeException.class})
    public String globalException(Exception e) {
        System.out.println("全局异常信息:" + e.getMessage());
        request.setAttribute("exMsg", e.getMessage());
        return "exception_mes";
    }

    @ExceptionHandler(ArithmeticException.class)
    public String arithmeticException(ArithmeticException e) {
        System.out.println("算术异常信息:" + e.getMessage());
        request.setAttribute("exMsg", e.getMessage());
        return "exception_mes";
    }
}
